package zhoufu.sz.sanxiao_check.ui;

import android.content.Context;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.widget.PopupMenu;
import android.support.v7.widget.SearchView;
import android.view.Menu;
import android.view.MenuItem;
import android.view.inputmethod.InputMethodManager;

import zhoufu.sz.sanxiao_check.R;
import zhoufu.sz.sanxiao_check.inject.HasComponent;
import zhoufu.sz.sanxiao_check.inject.components.DaggerLoginComponet;
import zhoufu.sz.sanxiao_check.inject.components.LoginComponet;
import zhoufu.sz.sanxiao_check.ui.fragment.StreetListFragment;

/**
 * User:RCP
 * email:ruancaipu@gmail.com
 * Date:2016/7/27
 */
public class SearchActivity extends BaseActivity implements HasComponent<LoginComponet> {

    LoginComponet mComponet;

    private int Type = 0;
    private SearchView searchView;

    @Override
    protected void initInject() {
        setTitle(R.string.title_search);
        mComponet = DaggerLoginComponet.builder()
                .activityModule(getActivityModule())
                .appComponent(getAppComponent())
                .build();
    }

    @Override
    protected int setIndividualId() {
        return R.layout.layout_frame;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_search, menu);
        MenuItem menuItem = menu.findItem(R.id.menu_search);
        searchView = (SearchView) menuItem.getActionView();
        menuItem.expandActionView();
        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                if (searchView != null) {
                    InputMethodManager imm =
                            (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                    if (imm != null) {
                        imm.hideSoftInputFromWindow(searchView.getWindowToken(), 0);
                    }
                    searchView.clearFocus();
                }
                FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
                switch (Type) {
                    case 0:
                        transaction.replace(R.id.main_fram,
                                StreetListFragment.newInstance(query, "0", "0", "0"));
                        break;
                    case 1:
                        transaction.replace(R.id.main_fram,
                                StreetListFragment.newInstance("0", query, "0", "0"));
                        break;
                    case 2:
                        transaction.replace(R.id.main_fram,
                                StreetListFragment.newInstance("0", "0", query, "0"));
                        break;
                    case 3:
                        transaction.replace(R.id.main_fram,
                                StreetListFragment.newInstance("0", "0", "0", query));
                        break;
                }
                transaction.commitAllowingStateLoss();
                return true;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                return false;
            }
        });
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == R.id.menu_search_type) {
            showFilteringPopUpMenu(item);
        }
        return super.onOptionsItemSelected(item);
    }

    public void showFilteringPopUpMenu(final MenuItem mitem) {
        PopupMenu popup = new PopupMenu(this, findViewById(R.id.menu_search_type));
        popup.getMenuInflater().inflate(R.menu.popmenu_search, popup.getMenu());
        popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
            public boolean onMenuItemClick(MenuItem item) {
                switch (item.getItemId()) {
                    case R.id.menu_search_1:
                        Type = 0;
                        break;
                    case R.id.menu_search_2:
                        Type = 1;
                        break;
                    case R.id.menu_search_3:
                        searchView.onActionViewCollapsed();
                        getSupportFragmentManager().beginTransaction().replace(R.id.main_fram,
                                StreetListFragment.newInstance("0", "0", "0", "0", "1")).commitAllowingStateLoss();
                        break;
                    case R.id.menu_search_4:
                        searchView.onActionViewCollapsed();
                        getSupportFragmentManager().beginTransaction().replace(R.id.main_fram,
                                StreetListFragment.newInstance("0", "0", "0", "0", "2")).commitAllowingStateLoss();
                        break;
                    case R.id.menu_search_5:
                        searchView.onActionViewCollapsed();
                        getSupportFragmentManager().beginTransaction().replace(R.id.main_fram,
                                StreetListFragment.newInstance("0", "0", "0", "0", "3")).commitAllowingStateLoss();
                        break;
                    case R.id.menu_search_6:
                        searchView.onActionViewCollapsed();
                        getSupportFragmentManager().beginTransaction().replace(R.id.main_fram,
                                StreetListFragment.newInstance("0", "0", "0", "0", "4")).commitAllowingStateLoss();
                        break;
                    case R.id.menu_search_7:
                        searchView.onActionViewCollapsed();
                        getSupportFragmentManager().beginTransaction().replace(R.id.main_fram,
                                StreetListFragment.newInstance("0", "0", "0", "0", "5")).commitAllowingStateLoss();
                        break;
                    case R.id.menu_search_8:
                        searchView.onActionViewCollapsed();
                        getSupportFragmentManager().beginTransaction().replace(R.id.main_fram,
                                StreetListFragment.newInstance("0", "0", "0", "0", "6")).commitAllowingStateLoss();
                        break;
                    case R.id.menu_search_9:
                        searchView.onActionViewCollapsed();
                        getSupportFragmentManager().beginTransaction().replace(R.id.main_fram,
                                StreetListFragment.newInstance("0", "0", "0", "0", "7")).commitAllowingStateLoss();
                        break;
                    case R.id.menu_search_10:
                        searchView.onActionViewCollapsed();
                        getSupportFragmentManager().beginTransaction().replace(R.id.main_fram,
                                StreetListFragment.newInstance("0", "0", "0", "0", "8")).commitAllowingStateLoss();
                        break;
                    case R.id.menu_search_11:
                        searchView.onActionViewCollapsed();
                        getSupportFragmentManager().beginTransaction().replace(R.id.main_fram,
                                StreetListFragment.newInstance("0", "0", "0", "0", "9")).commitAllowingStateLoss();
                        break;
                    case R.id.menu_search_12:
                        searchView.onActionViewCollapsed();
                        getSupportFragmentManager().beginTransaction().replace(R.id.main_fram,
                                StreetListFragment.newInstance("0", "0", "0",
                                        "0", "10")).commitAllowingStateLoss();
                        break;
                    case R.id.menu_search_13:
                        searchView.onActionViewCollapsed();
                        getSupportFragmentManager().beginTransaction().replace(R.id.main_fram,
                                StreetListFragment.newInstance("0", "0", "0",
                                        "0", "11")).commitAllowingStateLoss();
                        break;
                    case R.id.menu_search_14:
                        searchView.onActionViewCollapsed();
                        getSupportFragmentManager().beginTransaction().replace(R.id.main_fram,
                                StreetListFragment.newInstance("0", "0", "0",
                                        "0", "12")).commitAllowingStateLoss();
                        break;
                    case R.id.menu_search_15:
                        searchView.onActionViewCollapsed();
                        getSupportFragmentManager().beginTransaction().replace(R.id.main_fram,
                                StreetListFragment.newInstance("0", "0", "0",
                                        "0", "13")).commitAllowingStateLoss();
                        break;
                    case R.id.menu_search_16:
                        searchView.onActionViewCollapsed();
                        getSupportFragmentManager().beginTransaction().replace(R.id.main_fram,
                                StreetListFragment.newInstance("0", "0", "0",
                                        "0", "14")).commitAllowingStateLoss();
                        break;
                    case R.id.menu_search_17:
                        searchView.onActionViewCollapsed();
                        getSupportFragmentManager().beginTransaction().replace(R.id.main_fram,
                                StreetListFragment.newInstance("0", "0", "0",
                                        "0", "15")).commitAllowingStateLoss();
                        break;
                    case R.id.menu_search_18:
                        searchView.onActionViewCollapsed();
                        getSupportFragmentManager().beginTransaction().replace(R.id.main_fram,
                                StreetListFragment.newInstance("0", "0", "0",
                                        "0", "16")).commitAllowingStateLoss();
                        break;
                    case R.id.menu_search_19:
                        searchView.onActionViewCollapsed();
                        getSupportFragmentManager().beginTransaction().replace(R.id.main_fram,
                                StreetListFragment.newInstance("0", "0", "0",
                                        "0", "17")).commitAllowingStateLoss();
                        break;
                    case R.id.menu_search_20:
                        searchView.onActionViewCollapsed();
                        getSupportFragmentManager().beginTransaction().replace(R.id.main_fram,
                                StreetListFragment.newInstance("0", "0", "0",
                                        "0", "18")).commitAllowingStateLoss();
                        break;
                    case R.id.menu_search_21:
                        searchView.onActionViewCollapsed();
                        getSupportFragmentManager().beginTransaction().replace(R.id.main_fram,
                                StreetListFragment.newInstance("0", "0", "0",
                                        "0", "19")).commitAllowingStateLoss();
                        break;
                    //case R.id.menu_search_3:
                    //    Type = 2;
                    //    break;
                    //case R.id.menu_search_4:
                    //    Type = 3;
                    //    break;
                }
                mitem.setTitle(item.getTitle());
                return true;
            }
        });
        popup.show();
    }

    @Override
    public LoginComponet getComponent() {
        return mComponet;
    }
}
